<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="专题名称：">
            <el-input
              v-model="listQuery.name"
              class="input-width"
              placeholder="输入名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()"
        >新增专题</el-button
      >
      <el-button
        size="mini"
        class="btn-add"
        @click="infoAdd()"
        style="margin-right: 10px"
        >学院简介</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column label="ID" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="专题名称" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="排序设置" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="最近更新" width="160" align="center">
          <template slot-scope="scope">{{
            scope.row.updateTime | formatDateTime
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="280" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleVideoList(scope.$index, scope.row)"
              >视频内容
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handlePaper(scope.$index, scope.row)"
              >考题管理
            </el-button>
            <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--@click="handleModel(scope.$index, scope.row)"-->
            <!--&gt;证书模板-->
            <!--</el-button>-->
            <el-button
              size="mini"
              type="text"
              @click="handleReview(scope.$index, scope.row)"
              >查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)"
              >编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { collegeList, deleteId } from '@/api/college'
import { formatDate } from '@/utils/date'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
}
export default {
  name: 'CourseList',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      typeOptions: Object.assign({}),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      operates: [
        {
          label: '删除',
          value: 0
        }
      ],
      operateType: null,
      courseTypeOptions: [],
      loading: false,
      deptOptions: []
    }
  },
  created () {
    // this.getCourseType()
    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    infoAdd () {
      this.$router.push({ path: '/college-ms/info' })
    },
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.deleteCouerse(row.id)
    },
    handleVideoList (index, row) {
      this.$router.push({
        path: '/college-ms/collegeContent',
        query: { id: row.id, name: row.name }
      })
    },
    handleReview (index, row) {
      this.$router.push({
        path: '/college-ms/collegeDetails',
        query: { id: row.id }
      })
    },
    handleAdd () {
      this.$router.push({ path: '/college-ms/collegeAdd' })
    },
    handleModel (index, row) {
      this.$router.push({ path: '/college-ms/collegeModel', query: { id: row.id } })
    },
    handlePaper (index, row) {
      this.$router.push({
        path: '/college-ms/collegePaper',
        query: { id: row.id, examId: row.examId }
      })
    },
    handleUpdate (index, row) {
      this.$router.push({
        path: '/college-ms/collegeUpdate',
        query: { id: row.id }
      })
    },
    getList () {
      this.listLoading = true
      const { courseType, deptId } = this.listQuery
      const params = {
        ...this.listQuery
      }
      if (courseType && courseType.length > 0) {
        params.courseType = courseType[courseType.length - 1]
      }
      if (deptId) {
        params.deptName = deptId
      }
      collegeList(params).then((response) => {
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    deleteCouerse (ids) {
      this.$confirm('是否要删除该课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = new URLSearchParams()
        params.append('id', ids)
        deleteId(params).then((response) => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
